package com.qfedu.service.impl;

import com.qfedu.dao.GoodsInfoRepository;
import com.qfedu.pojo.entity.GoodsInfo;
import com.qfedu.pojo.req.GoodsInfoReq;
import com.qfedu.pojo.resp.BaseResp;
import com.qfedu.service.GoodsInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName GoodsInfoServiceImpl
 * @Description TODO
 * @Author young
 * @Date 2022-09-26 22:52
 * @Version 1.0
 **/
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {
    @Autowired
    GoodsInfoRepository goodsInfoRepository;


    @Override
    public BaseResp findByGoodsName(String goodsName){
        List<GoodsInfo> byGoodsName = goodsInfoRepository.findByGoodsnameLike(goodsName);
        return new BaseResp(1,"success",byGoodsName,null);
    }

    @Override
    public BaseResp findByType(Integer secondTypeId) {
        List<GoodsInfo> all = goodsInfoRepository.findByType(secondTypeId);
        return new BaseResp(1,"success",all,null);
    }

    @Override
    public BaseResp findById(Integer goodsId) {
        Optional<GoodsInfo> byId = goodsInfoRepository.findById(goodsId);
        return new BaseResp(1,"success",byId.get(),null);
    }

    @Override
    public BaseResp findByHotId(Integer hotId) {
        List<GoodsInfo> byHotId = goodsInfoRepository.findByHotId(hotId);
        return new BaseResp(1,"success",byHotId,null);
    }


    @Override
    public BaseResp findAll(Integer page, Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);
        Page<GoodsInfo> all = goodsInfoRepository.findAll(of);
        return new BaseResp(1,"success",all.getContent(),all.getTotalElements());
    }

    @Override
    public BaseResp delById(Integer goodsId) {
        goodsInfoRepository.deleteById(goodsId);
        return new BaseResp().Success();
    }

    @Override
    public BaseResp saveOrUpdate(GoodsInfoReq goodsInfoReq) {
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(goodsInfoReq,goodsInfo);
        goodsInfoRepository.saveAndFlush(goodsInfo);
        return new BaseResp().Success();
    }
}
